#!/usr/bin/env python
# -*- coding: utf-8 -*-
#===============================================================================
# Copyright 2011 zod.yslin
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# 
# Author: zod.yslin
# Email: 
# File Name: method.py
# Description: 
#   0. instance method
#   1. class method
#   2. static method
# Edit History: 
#   2011-08-10    File created.
#===============================================================================
class Demo:
    def __init__(self, i):
        self.i = i
     
    def __str__(self):
        return str(self.i)
          
    def instancetest(self):
        print("this is instance method..", self.i)
 
    def statictest1():
        print("this is static method..")
    
    # 建立 static 方法    
    statictest1 = staticmethod(statictest1)

    # 建立 static 方法    
    @staticmethod
    def statictest2():
        print("this is static method..")
     
     
    def classtest1(cls):
        """
        類別方法需要一個特別的參數 (parameter) ，
        習慣上使用 cls ，這與實體方法的 self 類似，
        不同的是 cls 用來存取類別的屬性 (attribute) 。
        """
        print("this is class method..")
        print("the class name is", cls.__name__)

    # 建立 class 方法
    classtest1 = classmethod(classtest1)

    # 建立 class 方法
    @classmethod
    def classtest2(cls):
        print("this is class method..")
        print("the class name is", cls.__name__)
    
 
Demo.statictest1()
Demo.statictest2()
Demo.classtest1()
Demo.classtest2()
print

a = Demo(9527)
a.instancetest()
a.statictest1()
a.statictest2()
a.classtest1()
a.classtest2()
